<!doctype html>




<html class="theme-next pisces" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
    
  
  <link href="//cdn.staticfile.org/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  
    
      
    

    
  

  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.lug.ustc.edu.cn/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|PT Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






  

<link href="//cdn.staticfile.org/font-awesome/4.6.2/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Web,JavaScript," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="用户代理字符串检测技术首先要确定的往往是你需要多么具体的浏览器信息。一般情况下，知道呈现引擎和最低限度的版本就足以决定正确的操作方法了。不推荐的：
123if (isIE6 || isIE7) &amp;#123; //不推荐!!!//代码&amp;#125;
这种代码其实是很脆弱的，因为它要依据特定的版本来决定做什么。如果是IE8 怎么办呢？只要IE 有新版本出来，就必须更新这些代码。
使用这类代码可以避免：">
<meta property="og:type" content="article">
<meta property="og:title" content="《 JavaScript 高级程序设计》备忘-第九章-2">
<meta property="og:url" content="http://codingme.xyz/2016/10/02/javascript-note-09-2/index.html">
<meta property="og:site_name" content="CodingMe">
<meta property="og:description" content="用户代理字符串检测技术首先要确定的往往是你需要多么具体的浏览器信息。一般情况下，知道呈现引擎和最低限度的版本就足以决定正确的操作方法了。不推荐的：
123if (isIE6 || isIE7) &amp;#123; //不推荐!!!//代码&amp;#125;
这种代码其实是很脆弱的，因为它要依据特定的版本来决定做什么。如果是IE8 怎么办呢？只要IE 有新版本出来，就必须更新这些代码。
使用这类代码可以避免：">
<meta property="og:updated_time" content="2016-10-18T06:23:42.128Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="《 JavaScript 高级程序设计》备忘-第九章-2">
<meta name="twitter:description" content="用户代理字符串检测技术首先要确定的往往是你需要多么具体的浏览器信息。一般情况下，知道呈现引擎和最低限度的版本就足以决定正确的操作方法了。不推荐的：
123if (isIE6 || isIE7) &amp;#123; //不推荐!!!//代码&amp;#125;
这种代码其实是很脆弱的，因为它要依据特定的版本来决定做什么。如果是IE8 怎么办呢？只要IE 有新版本出来，就必须更新这些代码。
使用这类代码可以避免：">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"right","display":"post"},
    fancybox: true,
    motion: false,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://codingme.xyz/2016/10/02/javascript-note-09-2/"/>





  <title> 《 JavaScript 高级程序设计》备忘-第九章-2 | CodingMe </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">CodingMe</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>


    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://codingme.xyz/2016/10/02/javascript-note-09-2/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Huang SJ">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="/uploads/avatar.jpg">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="CodingMe">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="CodingMe" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                《 JavaScript 高级程序设计》备忘-第九章-2
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            

            

            
          </span>

          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="用户代理字符串检测技术"><a href="#用户代理字符串检测技术" class="headerlink" title="用户代理字符串检测技术"></a>用户代理字符串检测技术</h1><p>首先要确定的往往是你需要多么具体的浏览器信息。<br>一般情况下，知道呈现引擎和最低限度的版本就足以决定正确的操作方法了。<br>不推荐的：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> (isIE6 || isIE7) &#123; <span class="comment">//不推荐!!!</span></div><div class="line"><span class="comment">//代码</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>这种代码其实是很脆弱的，因为它要依据特定的版本来决定做什么。<br>如果是IE8 怎么办呢？只要IE 有新版本出来，就必须更新这些代码。</p>
<p>使用这类代码可以避免：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> (ieVer &gt;=<span class="number">6</span>)&#123;</div><div class="line"><span class="comment">//代码</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<a id="more"></a>
<h2 id="识别呈现引擎"><a href="#识别呈现引擎" class="headerlink" title="识别呈现引擎"></a>识别呈现引擎</h2><p>主要检测五大呈现引擎：IE、Gecko、WebKit、KHTML 和Opera。<br>使用模块增强模式来封装检测脚本。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> client = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">    <span class="keyword">var</span> engine = &#123;</div><div class="line">        <span class="comment">//呈现引擎</span></div><div class="line">        ie: <span class="number">0</span>,</div><div class="line">        gecko: <span class="number">0</span>,</div><div class="line">        webkit: <span class="number">0</span>,</div><div class="line">        khtml: <span class="number">0</span>,</div><div class="line">        opera: <span class="number">0</span>,</div><div class="line">        <span class="comment">//具体的版本号</span></div><div class="line">        ver: <span class="literal">null</span></div><div class="line">    &#125;;</div><div class="line">    <span class="comment">//在此检测呈现引擎、平台和设备</span></div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        engine : engine</div><div class="line">    &#125;;</div><div class="line">&#125;();</div><div class="line"></div><div class="line"><span class="keyword">if</span> (client.engine.ie) &#123; <span class="comment">//如果是IE，client.ie 的值应该大于0</span></div><div class="line">    <span class="comment">//针对IE 的代码</span></div><div class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (client.engine.gecko &gt; <span class="number">1.5</span>)&#123;</div><div class="line">    <span class="keyword">if</span> (client.engine.ver == <span class="string">"1.8.1"</span>)&#123;</div><div class="line">        <span class="comment">//针对这个版本执行某些操作</span></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>声明了一个名为client 的全局变量，用于保存相关信息。<br>匿名函数内部定义了一个局部变量engine，它是一个包含默认设置的对象字面量。<br>每个呈现引擎都对应着一个属性，属性的值默认为0。<br>如果检测到了哪个呈现引擎，那么就以浮点数值形式将该引擎的版本号写入相应的属性。<br>而呈现引擎的完整版本(是一个字符串)，则被写入ver 属性。<br>在检测到一个呈现引擎之后，其client.engine 中对应的属性将被设置为一个大于0 的值，该值可以转换成布尔值true。<br>鉴于每个属性都包含一个浮点数值，因此有可能丢失某些版本信息。例如，将字符串”1.8.1”传入parseFloat()后会得到数值1.8。<br>不过，在必要的时候可以检测ver 属性，该属性中会保存完整的版本信息。</p>
<p>要正确地识别呈现引擎，关键是检测顺序要正确。</p>
<ol>
<li>首先要检测 Opera。检测什么 opera …已经没人用的浏览器有什么好检测的…</li>
<li><p>放在第二位检测的呈现引擎是WebKit。<br> WebKit 的用户代理字符串中包含”Gecko”和”KHTML”这两个子字符串，所以如果首先检测它们，很可能会得出错误的结论。<br> WebKit 的用户代理字符串中的”AppleWebKit”是独一无二的，因此检测这个字符串最合适。</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> ua = navigator.userAgent;</div><div class="line"><span class="keyword">if</span> (<span class="regexp">/AppleWebKit\/(\S+)/</span>.test(ua))&#123;</div><div class="line">    engine.ver = <span class="built_in">RegExp</span>[<span class="string">"$1"</span>];</div><div class="line">    engine.webkit = <span class="built_in">parseFloat</span>(engine.ver);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p> 首先将用户代理字符串保存在变量ua 中。然后通过正则表达式来测试其中是否包含字符串”AppleWebKit”，并使用捕获组来取得版本号。<br> 由于实际的版本号中可能会包含数字、小数点和字母，所以捕获组中使用了表示非空格的特殊字符(\S)。<br> 用户代理字符串中的版本号与下一部分的分隔符是一个空格，因此这个模式可以保证捕获所有版本信息。</p>
</li>
<li><p>测试呈现引擎KHTML。</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> ua = navigator.userAgent;</div><div class="line">……</div><div class="line"><span class="keyword">if</span> (<span class="regexp">/KHTML\/(\S+)/</span>.test(ua) || <span class="regexp">/Konqueror\/([^;]+)/</span>.test(ua))&#123;</div><div class="line">    engine.ver = <span class="built_in">RegExp</span>[<span class="string">"$1"</span>];</div><div class="line">    engine.khtml = <span class="built_in">parseFloat</span>(engine.ver);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p> 由于KHTML 的版本号与后继的标记之间有一个空格，因此仍然要使用特殊的非空格字符来取得与版本有关的所有字符。<br> 将字符串形式的版本信息保存在engine.ver 中，将浮点数值形式的版本保存在engin.khtml 中。<br> 如果KHTML 不在用户代理字符串中，那么就要匹配Konqueror后跟一个斜杠，再后跟不包含分号的所有字符。</p>
</li>
<li><p>测试检测 Gecko。<br> 在用户代理字符串中，Gecko的版本号不会出现在字符串”Gecko”的后面，而是会出现在字符串”rv:”的后面。</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> ua = navigator.userAgent;</div><div class="line">……</div><div class="line"><span class="keyword">if</span> (<span class="regexp">/rv:([^\)]+)\) Gecko\/\d&#123;8&#125;/</span>.test(ua))&#123;</div><div class="line">    engine.ver = <span class="built_in">RegExp</span>[<span class="string">"$1"</span>];</div><div class="line">    engine.gecko = <span class="built_in">parseFloat</span>(engine.ver);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p> Gecko 的版本号位于字符串”rv:”与一个闭括号之间，因此为了提取出这个版本号，正则表达式要查找所有不是闭括号的字符，还要查找字符串”Gecko/“后跟8 个数字。</p>
</li>
<li><p>检测呈现引擎 IE。</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> ua = navigator.userAgent;</div><div class="line">……</div><div class="line"><span class="keyword">else</span> <span class="keyword">if</span> (<span class="regexp">/MSIE ([^;]+)/</span>.test(ua))&#123;</div><div class="line">    engine.ver = <span class="built_in">RegExp</span>[<span class="string">"$1"</span>];</div><div class="line">    engine.ie = <span class="built_in">parseFloat</span>(engine.ver);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p> 在正则表达式中使用取反的字符类来取得不是分号的所有字符。<br> IE 通常会保证以标准浮点数值形式给出其版本号，但有时候也不一定。<br> 因此，取反的字符类[^;]可以确保取得多个小数点以及任何可能的字符。</p>
</li>
</ol>
<h2 id="识别浏览器"><a href="#识别浏览器" class="headerlink" title="识别浏览器"></a>识别浏览器</h2><p>苹果公司的Safari 浏览器和谷歌公司的Chrome 浏览器都使用WebKit 作为呈现引擎，但它们的JavaScript 引擎却不一样。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> client = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">    <span class="keyword">var</span> engine = &#123;</div><div class="line">            ……</div><div class="line">        &#125;;</div><div class="line">    <span class="keyword">var</span> browser = &#123;</div><div class="line">        <span class="comment">//浏览器</span></div><div class="line">        ie: <span class="number">0</span>,</div><div class="line">        firefox: <span class="number">0</span>,</div><div class="line">        safari: <span class="number">0</span>,</div><div class="line">        konq: <span class="number">0</span>,</div><div class="line">        opera: <span class="number">0</span>,</div><div class="line">        chrome: <span class="number">0</span>,</div><div class="line">        <span class="comment">//具体的版本</span></div><div class="line">        ver: <span class="literal">null</span></div><div class="line">    &#125;;</div><div class="line">    <span class="comment">//在此检测呈现引擎、平台和设备</span></div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        engine: engine,</div><div class="line">        browser: browser</div><div class="line">    &#125;;</div><div class="line">&#125;();</div></pre></td></tr></table></figure>
<p>代码中又添加了私有变量browser，用于保存每个主要浏览器的属性。<br>与engine 变量一样，除了当前使用的浏览器，其他属性的值将保持为0；<br>如果是当前使用的浏览器，则这个属性中保存的是浮点数值形式的版本号。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div></pre></td><td class="code"><pre><div class="line">……</div><div class="line"><span class="comment">//确定是Chrome 还是Safari</span></div><div class="line"><span class="keyword">if</span> (<span class="regexp">/Chrome\/(\S+)/</span>.test(ua))&#123;</div><div class="line">    browser.ver = <span class="built_in">RegExp</span>[<span class="string">"$1"</span>];</div><div class="line">    browser.chrome = <span class="built_in">parseFloat</span>(browser.ver);</div><div class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="regexp">/Version\/(\S+)/</span>.test(ua))&#123;</div><div class="line">    browser.ver = <span class="built_in">RegExp</span>[<span class="string">"$1"</span>];</div><div class="line">    browser.safari = <span class="built_in">parseFloat</span>(browser.ver);</div><div class="line">&#125; <span class="keyword">else</span> &#123;</div><div class="line">    <span class="comment">//近似地确定版本号</span></div><div class="line">    <span class="keyword">var</span> safariVersion = <span class="number">1</span>;</div><div class="line">    <span class="keyword">if</span> (engine.webkit &lt; <span class="number">100</span>)&#123;</div><div class="line">        safariVersion = <span class="number">1</span>;</div><div class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (engine.webkit &lt; <span class="number">312</span>)&#123;</div><div class="line">        safariVersion = <span class="number">1.2</span>;</div><div class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (engine.webkit &lt; <span class="number">412</span>)&#123;</div><div class="line">        safariVersion = <span class="number">1.3</span>;</div><div class="line">    &#125; <span class="keyword">else</span> &#123;</div><div class="line">        safariVersion = <span class="number">2</span>;</div><div class="line">    &#125;</div><div class="line">    browser.safari = browser.ver = safariVersion;</div><div class="line">&#125;</div><div class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="regexp">/KHTML\/(\S+)/</span>.test(ua) || <span class="regexp">/Konqueror\/([^;]+)/</span>.test(ua))&#123;</div><div class="line">    engine.ver = browser.ver = <span class="built_in">RegExp</span>[<span class="string">"$1"</span>];</div><div class="line">    engine.khtml = browser.konq = <span class="built_in">parseFloat</span>(engine.ver);</div><div class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="regexp">/rv:([^\)]+)\) Gecko\/\d&#123;8&#125;/</span>.test(ua))&#123;</div><div class="line">    engine.ver = <span class="built_in">RegExp</span>[<span class="string">"$1"</span>];</div><div class="line">    engine.gecko = <span class="built_in">parseFloat</span>(engine.ver);</div><div class="line"><span class="comment">//确定是不是Firefox</span></div><div class="line"><span class="keyword">if</span> (<span class="regexp">/Firefox\/(\S+)/</span>.test(ua))&#123;</div><div class="line">    browser.ver = <span class="built_in">RegExp</span>[<span class="string">"$1"</span>];</div><div class="line">    browser.firefox = <span class="built_in">parseFloat</span>(browser.ver);</div><div class="line">&#125;</div><div class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="regexp">/MSIE ([^;]+)/</span>.test(ua))&#123;</div><div class="line">    engine.ver = browser.ver = <span class="built_in">RegExp</span>[<span class="string">"$1"</span>];</div><div class="line">    engine.ie = browser.ie = <span class="built_in">parseFloat</span>(engine.ver);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>之后就能针对浏览器编写代码了。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> (client.engine.webkit) &#123; <span class="comment">//if it’s WebKit</span></div><div class="line">    <span class="keyword">if</span> (client.browser.chrome)&#123;</div><div class="line">        <span class="comment">//执行针对Chrome 的代码</span></div><div class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (client.browser.safari)&#123;</div><div class="line">        <span class="comment">//执行针对Safari 的代码</span></div><div class="line">    &#125;</div><div class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (client.engine.gecko)&#123;</div><div class="line">    <span class="keyword">if</span> (client.browser.firefox)&#123;</div><div class="line">        <span class="comment">//执行针对Firefox 的代码</span></div><div class="line">    &#125; <span class="keyword">else</span> &#123;</div><div class="line">        <span class="comment">//执行针对其他Gecko 浏览器的代码</span></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="识别平台"><a href="#识别平台" class="headerlink" title="识别平台"></a>识别平台</h2><p>一样的老套路。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">……</div><div class="line">    <span class="keyword">var</span> system = &#123;</div><div class="line">        win: <span class="literal">false</span>,</div><div class="line">        mac: <span class="literal">false</span>,</div><div class="line">        x11: <span class="literal">false</span></div><div class="line">    &#125;;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        engine: engine,</div><div class="line">        browser: browser,</div><div class="line">        system: system</div><div class="line">    &#125;;</div><div class="line">&#125;();</div></pre></td></tr></table></figure>
<p>确定平台时，检测navigator.platform 要比检测用户代理字符串更简单，后者在不同浏览器中会给出不同的平台信息。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> p = navigator.platform;</div><div class="line">system.win = p.indexOf(<span class="string">"Win"</span>) == <span class="number">0</span>;</div><div class="line">system.mac = p.indexOf(<span class="string">"Mac"</span>) == <span class="number">0</span>;</div><div class="line">system.x11 = (p.indexOf(<span class="string">"X11"</span>) == <span class="number">0</span>) || (p.indexOf(<span class="string">"Linux"</span>) == <span class="number">0</span>);</div></pre></td></tr></table></figure>
<p>检测Unix 时，则同时检查了字符串”X11”和”Linux”。</p>
<h2 id="识别Windows-操作系统"><a href="#识别Windows-操作系统" class="headerlink" title="识别Windows 操作系统"></a>识别Windows 操作系统</h2><p>(话说浏览器JavaScript为啥需要识别到哪个Windows系统这么细..)</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> (system.win)&#123;</div><div class="line">    <span class="keyword">if</span> (<span class="regexp">/Win(?:dows )?([^do]&#123;2&#125;)\s?(\d+\.\d+)?/</span>.test(ua))&#123;</div><div class="line">        <span class="keyword">if</span> (<span class="built_in">RegExp</span>[<span class="string">"$1"</span>] == <span class="string">"NT"</span>)&#123;</div><div class="line">            <span class="keyword">switch</span>(<span class="built_in">RegExp</span>[<span class="string">"$2"</span>])&#123;</div><div class="line">                <span class="keyword">case</span> <span class="string">"5.0"</span>:</div><div class="line">                system.win = <span class="string">"2000"</span>;</div><div class="line">                <span class="keyword">break</span>;</div><div class="line">                <span class="keyword">case</span> <span class="string">"5.1"</span>:</div><div class="line">                system.win = <span class="string">"XP"</span>;</div><div class="line">                <span class="keyword">break</span>;</div><div class="line">                <span class="keyword">case</span> <span class="string">"6.0"</span>:</div><div class="line">                system.win = <span class="string">"Vista"</span>;</div><div class="line">                <span class="keyword">break</span>;</div><div class="line">                <span class="keyword">case</span> <span class="string">"6.1"</span>:</div><div class="line">                system.win = <span class="string">"7"</span>;</div><div class="line">                <span class="keyword">break</span>;</div><div class="line">                <span class="keyword">default</span>:</div><div class="line">                system.win = <span class="string">"NT"</span>;</div><div class="line">                <span class="keyword">break</span>;</div><div class="line">            &#125;</div><div class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">RegExp</span>[<span class="string">"$1"</span>] == <span class="string">"9x"</span>)&#123;</div><div class="line">            system.win = <span class="string">"ME"</span>;</div><div class="line">        &#125; <span class="keyword">else</span> &#123;</div><div class="line">            system.win = <span class="built_in">RegExp</span>[<span class="string">"$1"</span>];</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">if</span> (client.system.win)&#123;</div><div class="line">    <span class="keyword">if</span> (client.system.win == <span class="string">"XP"</span>) &#123;</div><div class="line">        <span class="comment">//说明是XP</span></div><div class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (client.system.win == <span class="string">"Vista"</span>)&#123;</div><div class="line">        <span class="comment">//说明是Vista</span></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="识别移动设备"><a href="#识别移动设备" class="headerlink" title="识别移动设备"></a>识别移动设备</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div></pre></td><td class="code"><pre><div class="line">……</div><div class="line"><span class="keyword">var</span> system = &#123;</div><div class="line">    win: <span class="literal">false</span>,</div><div class="line">    mac: <span class="literal">false</span>,</div><div class="line">    x11: <span class="literal">false</span>,</div><div class="line">    <span class="comment">//移动设备</span></div><div class="line">    iphone: <span class="literal">false</span>,</div><div class="line">    ipod: <span class="literal">false</span>,</div><div class="line">    ipad: <span class="literal">false</span>,</div><div class="line">    ios: <span class="literal">false</span>,</div><div class="line">    android: <span class="literal">false</span>,</div><div class="line">    nokiaN: <span class="literal">false</span>,</div><div class="line">    winMobile: <span class="literal">false</span></div><div class="line">&#125;;</div><div class="line">……</div><div class="line"></div><div class="line">system.iphone = ua.indexOf(<span class="string">"iPhone"</span>) &gt; <span class="number">-1</span>;</div><div class="line">system.ipod = ua.indexOf(<span class="string">"iPod"</span>) &gt; <span class="number">-1</span>;</div><div class="line">system.ipod = ua.indexOf(<span class="string">"iPad"</span>) &gt; <span class="number">-1</span>;</div><div class="line"></div><div class="line"><span class="comment">//检测iOS 版本</span></div><div class="line"><span class="keyword">if</span> (system.mac &amp;&amp; ua.indexOf(<span class="string">"Mobile"</span>) &gt; <span class="number">-1</span>)&#123;</div><div class="line">    <span class="keyword">if</span> (<span class="regexp">/CPU (?:iPhone )?OS (\d+_\d+)/</span>.test(ua))&#123;</div><div class="line">        system.ios = <span class="built_in">parseFloat</span>(<span class="built_in">RegExp</span>.$<span class="number">1.</span>replace(<span class="string">"_"</span>, <span class="string">"."</span>));</div><div class="line">    &#125; <span class="keyword">else</span> &#123;</div><div class="line">        system.ios = <span class="number">2</span>; <span class="comment">//不能真正检测出来，所以只能猜测</span></div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//检测Android 版本</span></div><div class="line"><span class="keyword">if</span> (<span class="regexp">/Android (\d+\.\d+)/</span>.test(ua))&#123;</div><div class="line">    system.android = <span class="built_in">parseFloat</span>(<span class="built_in">RegExp</span>.$<span class="number">1</span>);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">if</span> (client.engine.webkit)&#123;</div><div class="line">    <span class="keyword">if</span> (client.system. iOS)&#123;</div><div class="line">        <span class="comment">//iOS 手机的内容</span></div><div class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (client.system.android)&#123;</div><div class="line">        <span class="comment">//Android 手机的内容</span></div><div class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (client.system.nokiaN)&#123;</div><div class="line">        <span class="comment">//诺基亚手机的内容</span></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="识别游戏系统"><a href="#识别游戏系统" class="headerlink" title="识别游戏系统"></a>识别游戏系统</h2><p>Wii 中的浏览器实际上是定制版的Opera：<code>Opera/9.10 (Nintendo Wii;U; ; 1621; en)</code><br>PS3 中的浏览器：<code>Mozilla/5.0 (PLAYSTATION 3; 2.00)</code></p>
<p><code>system.wii = ua.indexOf(&quot;Wii&quot;) &gt; -1;</code><br><code>system.ps = /playstation/i.test(ua);</code></p>
<h1 id="使用情况"><a href="#使用情况" class="headerlink" title="使用情况"></a>使用情况</h1><p>用户代理检测是客户端检测的最后一个选择。只要可能，都应该优先采用能力检测和怪癖检测。<br>用户代理检测一般适用于下列情形。</p>
<ul>
<li>不能直接准确地使用能力检测或怪癖检测。例如，某些浏览器实现了为将来功能预留的存根(stub)函数。</li>
<li>同一款浏览器在不同平台下具备不同的能力。这时候，可能就有必要确定浏览器位于哪个平台下。</li>
<li>为了跟踪分析等目的需要知道确切的浏览器。</li>
</ul>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Web/" rel="tag"># Web</a>
          
            <a href="/tags/JavaScript/" rel="tag"># JavaScript</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/09/30/javascript-note-08-2/" rel="next" title="《 JavaScript 高级程序设计》备忘-第八章-2">
                <i class="fa fa-chevron-left"></i> 《 JavaScript 高级程序设计》备忘-第八章-2
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/10/02/javascript-note-10-1/" rel="prev" title="《 JavaScript 高级程序设计》备忘-第十章-1">
                《 JavaScript 高级程序设计》备忘-第十章-1 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/uploads/avatar.jpg"
               alt="Huang SJ" />
          <p class="site-author-name" itemprop="name">Huang SJ</p>
          <p class="site-description motion-element" itemprop="description"></p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">75</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">4</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">9</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hsj1992" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/hsj9527" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/u/1694147905" target="_blank" title="Weibo">
                  
                    <i class="fa fa-fw fa-weibo"></i>
                  
                  Weibo
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.freecodecamp.cn/hsj1992" target="_blank" title="FreeCodeCamp">
                  
                    <i class="fa fa-fw fa-fire"></i>
                  
                  FreeCodeCamp
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://codepen.io/hsj1992/" target="_blank" title="codepen">
                  
                    <i class="fa fa-fw fa-codepen"></i>
                  
                  codepen
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://hsj1992.oschina.io/" target="_blank" title="码云镜像">
                  
                    <i class="fa fa-fw fa-code"></i>
                  
                  码云镜像
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://hsj1992.coding.me/" target="_blank" title="coding镜像">
                  
                    <i class="fa fa-fw fa-code"></i>
                  
                  coding镜像
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://hsj1992.github.io/" target="_blank" title="github镜像">
                  
                    <i class="fa fa-fw fa-code"></i>
                  
                  github镜像
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://codingme-6dd8a.firebaseapp.com" target="_blank" title="Firebase镜像">
                  
                    <i class="fa fa-fw fa-google"></i>
                  
                  Firebase镜像
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#用户代理字符串检测技术"><span class="nav-number">1.</span> <span class="nav-text">用户代理字符串检测技术</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#识别呈现引擎"><span class="nav-number">1.1.</span> <span class="nav-text">识别呈现引擎</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#识别浏览器"><span class="nav-number">1.2.</span> <span class="nav-text">识别浏览器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#识别平台"><span class="nav-number">1.3.</span> <span class="nav-text">识别平台</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#识别Windows-操作系统"><span class="nav-number">1.4.</span> <span class="nav-text">识别Windows 操作系统</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#识别移动设备"><span class="nav-number">1.5.</span> <span class="nav-text">识别移动设备</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#识别游戏系统"><span class="nav-number">1.6.</span> <span class="nav-text">识别游戏系统</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#使用情况"><span class="nav-number">2.</span> <span class="nav-text">使用情况</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Huang SJ</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="//cdn.staticfile.org/jquery/2.1.3/jquery.min.js"></script>

  
  <script type="text/javascript" src="//cdn.staticfile.org/fastclick/1.0.6/fastclick.min.js"></script>

  
  <script type="text/javascript" src="//cdn.staticfile.org/jquery_lazyload/1.9.7/jquery.lazyload.min.js"></script>

  
  <script type="text/javascript" src="//cdn.staticfile.org/velocity/1.2.1/velocity.min.js"></script>

  
  <script type="text/javascript" src="//cdn.staticfile.org/velocity/1.2.1/velocity.ui.min.js"></script>

  
  <script type="text/javascript" src="//cdn.staticfile.org/fancybox/2.1.5/jquery.fancybox.pack.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	




  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
      search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="popoverlay">').css('overflow', 'hidden');
      $('.popup').toggle();
    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';
      $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = $( "entry", xmlResponse ).map(function() {
            return {
              title: $( "title", this ).text(),
              content: $("content",this).text(),
              url: $( "url" , this).text()
            };
          }).get();
          var $input = document.getElementById(search_id);
          var $resultContent = document.getElementById(content_id);
          $input.addEventListener('input', function(){
            var matchcounts = 0;
            var str='<ul class=\"search-result-list\">';
            var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
            $resultContent.innerHTML = "";
            if (this.value.trim().length > 1) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var content_index = [];
                var data_title = data.title.trim().toLowerCase();
                var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                var data_url = decodeURIComponent(data.url);
                var index_title = -1;
                var index_content = -1;
                var first_occur = -1;
                // only match artiles with not empty titles and contents
                if(data_title != '') {
                  keywords.forEach(function(keyword, i) {
                    index_title = data_title.indexOf(keyword);
                    index_content = data_content.indexOf(keyword);
                    if( index_title >= 0 || index_content >= 0 ){
                      isMatch = true;
                      if (i == 0) {
                        first_occur = index_content;
                      }
                    }

                  });
                }
                // show search results
                if (isMatch) {
                  matchcounts += 1;
                  str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                  var content = data.content.trim().replace(/<[^>]+>/g,"");
                  if (first_occur >= 0) {
                    // cut out 100 characters
                    var start = first_occur - 20;
                    var end = first_occur + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if(start == 0){
                      end = 50;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    var match_content = content.substring(start, end);
                    // highlight all keywords
                    keywords.forEach(function(keyword){
                      var regS = new RegExp(keyword, "gi");
                      match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                    });

                    str += "<p class=\"search-result\">" + match_content +"...</p>"
                  }
                  str += "</li>";
                }
              })};
            str += "</ul>";
            if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
            if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
            $resultContent.innerHTML = str;
          });
          proceedsearch();
        }
      });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".popoverlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>


  

  

  

  


</body>
</html>
